<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型" prop="productType">
        <el-select v-model="queryParams.productType" placeholder="请选择商品类型" clearable>
          <el-option
            v-for="dict in dict.type.product_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出售类型" prop="productType">
        <el-select v-model="queryParams.conversionType" placeholder="请选择商品类型" clearable>
          <el-option
            v-for="dict in dict.type.conversion_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品描述" prop="productRemark">
        <el-input
          v-model="queryParams.productRemark"
          placeholder="请输入商品描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:productInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:productInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:productInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:productInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="商品金额" align="center" prop="productAmount" />
      <el-table-column label="商品类型" align="center" prop="productType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_type" :value="scope.row.productType"/>
        </template>
      </el-table-column>
      <el-table-column label="出售类型" align="center" prop="productType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.conversion_type" :value="scope.row.conversionType"/>
        </template>
      </el-table-column>
      <el-table-column label="商品积分" align="center" prop="productPoints" />
      <el-table-column label="商品数量" align="center" prop="productQuantity" />
      <el-table-column label="商品描述" align="center" prop="productRemark" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:productInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:productInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品金额" prop="productAmount">
          <el-input v-model="form.productAmount" placeholder="请输入商品金额" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品类型" prop="productType">
              <el-select v-model="form.productType" placeholder="请选择商品类型">
                <el-option
                  v-for="dict in dict.type.product_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类别归属" prop="productCategory">
              <el-select v-model="form.productCategory" placeholder="请选择商品类别归属">
                <el-option
                  v-for="dict in dict.type.product_category"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="出售类型" prop="productType">
          <el-select v-model="form.conversionType" placeholder="请选择商品出售类型">
            <el-option
              v-for="dict in dict.type.conversion_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品积分" prop="productPoints">
          <el-input v-model="form.productPoints" placeholder="请输入商品积分" />
        </el-form-item>
        <el-form-item label="商品数量" prop="productQuantity">
          <el-input v-model="form.productQuantity" placeholder="请输入商品数量" />
        </el-form-item>
        <el-form-item label="商品描述" prop="productRemark">
          <el-input v-model="form.productRemark" placeholder="请输入商品描述" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">商品图片信息</el-divider>

        <el-form-item label="商品主图" prop="remark">
          <image-upload v-model="form.mainUrl" :limit="5" />
        </el-form-item>

        <el-form-item label="商品详图" prop="remark">
          <image-upload v-model="form.infoUrl"  :limit="8" />
        </el-form-item>


<!--        <el-row :gutter="10" class="mb8">-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddProductFile">添加</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteProductFile">删除</el-button>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--        <el-table :data="productFileList" :row-class-name="rowProductFileIndex" @selection-change="handleProductFileSelectionChange" ref="productFile">-->
<!--          <el-table-column type="selection" width="50" align="center" />-->
<!--          <el-table-column label="序号" align="center" prop="index" width="50"/>-->
<!--          <el-table-column label="图片类型" prop="fileType" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-select v-model="scope.row.fileType" placeholder="请选择图片类型">-->
<!--                <el-option-->
<!--                  v-for="dict in dict.type.product_file_type"-->
<!--                  :key="dict.value"-->
<!--                  :label="dict.label"-->
<!--                  :value="dict.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="图片地址" prop="url" width="850">-->
<!--            <template slot-scope="scope">-->
<!--              <image-upload v-model="scope.row.url" :limit="1" placeholder="请输入图片地址" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--        </el-table>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProductInfo, getProductInfo, delProductInfo, addProductInfo, updateProductInfo } from "@/api/system/productInfo";

export default {
  name: "ProductInfo",
  dicts: ['product_type','conversion_type', 'product_file_type','product_category'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedProductFile: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品详情表格数据
      productInfoList: [],
      // 商品图片表格数据
      productFileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productAmount: null,
        productType: null,
        productCategory: null,
        conversionType: null,
        productPoints: null,
        productQuantity: null,
        productRemark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品详情列表 */
    getList() {
      this.loading = true;
      listProductInfo(this.queryParams).then(response => {
        this.productInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productName: null,
        productAmount: null,
        productType: null,
        productPoints: null,
        productQuantity: null,
        productRemark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        productCategory: null
      };
      this.productFileList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductInfo(id).then(response => {
        this.form = response.data;
        let mainUrl = "";
        let infoUrl = "";
        response.data.productFileList.forEach(item =>{
          if(item.fileType == 1){
            mainUrl = mainUrl+item.url+",";
          }
          if(item.fileType == 2){
            infoUrl = infoUrl+item.url+",";
          }
        })
        mainUrl = mainUrl.slice(0,mainUrl.length-1);
        infoUrl = infoUrl.slice(0,infoUrl.length-1);
        this.form.mainUrl = mainUrl;
        this.form.infoUrl = infoUrl;
        this.productFileList = response.data.productFileList;
        this.open = true;
        this.title = "修改商品详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.productFileList = [];
          if(!this.form.mainUrl){
            this.form.mainUrl.split(",").forEach(item=>{
              let mainImage = {};
              mainImage.url = item;
              mainImage.fileType = '1';
              this.form.productFileList.push(mainImage);
            });
          }
          if(this.form.infoUrl) {
            this.form.infoUrl.split(",").forEach(item => {
              let infoImage = {};
              infoImage.url = item;
              infoImage.fileType = '2';
              this.form.productFileList.push(infoImage);
            });
          }
          // this.form.productFileList = this.productFileList;
          if (this.form.id != null) {
            updateProductInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品详情编号为"' + ids + '"的数据项？').then(function() {
        return delProductInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 商品图片序号 */
    rowProductFileIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 商品图片添加按钮操作 */
    handleAddProductFile() {
      if(this.productFileList.length==5){
        alert("最多可上传五张图片");
      }else {
        let obj = {};
        obj.url = "";
        this.productFileList.push(obj);
      }
    },
    /** 商品图片删除按钮操作 */
    handleDeleteProductFile() {
      if (this.checkedProductFile.length == 0) {
        this.$modal.msgError("请先选择要删除的商品图片数据");
      } else {
        const productFileList = this.productFileList;
        const checkedProductFile = this.checkedProductFile;
        this.productFileList = productFileList.filter(function(item) {
          return checkedProductFile.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleProductFileSelectionChange(selection) {
      this.checkedProductFile = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/productInfo/export', {
        ...this.queryParams
      }, `productInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
